<?php
	/**
	 * @author		: Renato Miawaki
	 * @date		: 11/02/2012
	 * @version		: 1.0
	 * @tabela		: jogo
	 * @description		: 	
	 * 					id, 
	 * 					active, 
	 * 					category_id, 
	 * 					name, 
	 * 					content, 
	 * 					img_url
	 *
	 * @obs			: jogo
	 */
//importa classes de apoio
include_once "library/jogando/modules/jogo/dao/JogoDAO.class.php";
include_once "library/facil3/core/modules/comment/vo/BaseCommentVO.class.php";
include_once "library/facil3/interface/vo/PatternVO.class.php";

class JogoVO extends BaseCommentVO implements PatternVO{
	public $name;
	public $content;
	public $category_id;
	public $img_url;
	public $ranking;
	private $category_name;
	function __construct($arrayFetchPost = NULL, $locale = NULL){
		//inicia funo de validacao
		parent::_construct();
		$this->__table = "content";
		//se enviar $arrayFetchPost é para distrinchar internamente os dados do usuario
		if($arrayFetchPost !== NULL){
			//enviou array para distrinchar
			$this->setFetchArray($arrayFetchPost, $locale);
		}
	}//end construct
	/**
	 * inicia internamente a DAO
	 * @return void
	 */
	public function startDAO(){
		$this->DAO = JogoDAO::getInstance();
	}
	/**
	 * precisa enviar uma array dentro dos padr�es dessa VO, pode enviar diretamente o $_POST
	 * @param (array) $array_dados
	 * @return void
	 */
	function setFetchArray($array_dados, $locale = NULL){
		$this->id = DataHandler::getValueByArrayIndex($array_dados, "id");
		$this->active = DataHandler::getValueByArrayIndex($array_dados, "active");
		$this->setName(DataHandler::getValueByArrayIndex($array_dados, "name")					, $locale);
		$this->setContent(DataHandler::getValueByArrayIndex($array_dados, "content")			, $locale);
		$this->setImgURL(DataHandler::getValueByArrayIndex($array_dados, "img_url")			, $locale);
		$this->setCategoryId(DataHandler::getValueByArrayIndex($array_dados, "category_id"));
		$this->setRancking(DataHandler::getValueByArrayIndex($array_dados, "ranking"));
		
	}
	
	/**
	 * @param (object) $obj_dados
	 * @return void
	 */
	function setFetchObject($obj_dados){
		$this->id = DataHandler::getValueByStdObjectIndex($obj_dados, "id");
		$this->active = DataHandler::getValueByStdObjectIndex($obj_dados, "active");
		$this->name = DataHandler::getValueByStdObjectIndex($obj_dados, "name");
		$this->content = DataHandler::getValueByStdObjectIndex($obj_dados, "content");
		$this->setImgURL(DataHandler::getValueByStdObjectIndex($obj_dados, "img_url"));
		$this->setCategoryId(DataHandler::getValueByStdObjectIndex($obj_dados, "category_id"));
		$this->setRanking(DataHandler::getValueByStdObjectIndex($obj_dados, "ranking"));
	}
	

	//---------------------------------------------------------  SETs
	
	/**
	 * @param int active
	 */
	//public function setActive($active){
	//	if($active !== NULL){
	//		$this->active = DataHandler::forceInt($active);
	//	}
	//}
	/**
	 * @param $name
	 * @param $locale
	 * @return void
	 */
	public function setName($name, $locale = NULL){
		if($locale == NULL){
			$this->name = DataHandler::forceString($name);
		} else {
			$this->addTherms(self::TERM_NAME, $locale, $name);
		}
	}
	/**
	 * @param $content
	 * @param $locale
	 * @return void
	 */
	public function setContent($content, $locale = NULL){
		if($locale == NULL){
			$this->content = DataHandler::forceString($content);
		} else {
			$this->addTherms(self::TERM_CONTENT, $locale, $content);
		}
	}
	/**
	 * @param $img_url
	 * @param $locale
	 * @return void
	 */
	public function setImgUrl($img_url){
		$this->img_url = $img_url;
	}
	/**
	 * @param (string)$category_id 
	 */
	public function setCategoryId($id){
		$this->category_id  = DataHandler::forceInt($id);
	}
	/**
	 * @param (string)$ranking 
	 */
	public function setRanking($value){
		$this->ranking  = DataHandler::forceInt($value);
	}
	/**
	 * @param $locale
	 * @return str
	 */
	public function getName($locale = NULL){
		if($locale == NULL){
			return $this->name;
		} else {
			$tempResult = $this->getStringTherm(ContentVO::TERM_NAME, $locale);
			return ($tempResult == "")?$this->name:$tempResult;
		}
	}
	/**
	 * @param $locale
	 * @return str
	 */
	public function getContent($locale = NULL){
		if($locale == NULL){
			return $this->content;
		} else {
			$tempResult = $this->getStringTherm(ContentVO::TERM_CONTENT, $locale);
			return ($tempResult == "")?$this->content:$tempResult;
		}
	}
	/**
	 * @return str
	 */
	public function getImgUrl(){
		return $this->img_url;
	}
	/**
	 * @return int
	 */
	public function getCategoryId(){
		return $this->category_id;
	}
	/**
	 * @return string
	 */
	public function getCategoryName(){
		if($this->category_id == NULL){
			return NULL;
		}
		if(!$this->category_name){
			$CategoryVO = new CategoryVO();
			$CategoryVO->setId($this->category_id, TRUE);
			$this->category_name = $CategoryVO->getName();
		}
		return $this->category_name;
	}
	/**
	 * @return int
	 */
	public function getRanking(){
		return $this->ranking;
	}
	/**
	 * Validação da VO para ver se está apta a ser cadastrada
	 * @return array
	 */
	function validate(){
		//vai validar a VO inteira conforme as regras
		$ReturnResultVO = new ReturnResultVO();
		$ReturnResultVO->success = TRUE;
		return $ReturnResultVO;
	}
	
	/**
	 * @param int $tempResult
	 * @param array $arrayReturn
	 */
	public function resultHandler($tempResult, &$arrayReturn){
		switch($tempResult){
			case DbInterface::ERROR_UPDATE_DONOT_HAVE_ID:
				$arrayReturn[] = "envie o id";
				break;
			case DbInterface::ERROR_UPDATE_DONOT_HAVE_UPDATES:
				$arrayReturn[] = "nada a atualizar";
				break;
			case DbInterface::ERROR_UPDATE_COMMIT:
				$arrayReturn[] = "erro no update";
				break;
			case DbInterface::ERROR_INSERT_COMMIT:
				$arrayReturn[] = "erro ao inserir";
				break;
			case DbInterface::SUCCESS:
				$arrayReturn[] = "jogo gravado com sucesso";
				break;
			default:
				$arrayReturn[] = "erro sinistro($tempResult).";
				break;
		}//end switch
	}
	
}